import time
import _thread
import websocket


def on_open(ws):
    def run(*args):
        for i in range(3):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
        print("thread terminating...")
    _thread.start_new_thread(run, ())


def on_message(ws, message):
    print(message)


def on_error(ws, error):
    print(error)


def on_close(ws):
    print("### closed ###")


if __name__ == "__main__":
    websocket.enableTrace(True)
    header_auth = 'Authorization: ' + 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlaWQiOiJUMSIsImluc2lkIjoxLCJ1bmlvbmlkIjoib20zWVZ3bmZOSXRxeEpCTXJpTFhybUI3UWNLdyIsIm5iZiI6MTUyOTA0OTkwNiwiaXNzIjoiWmxuRUJHbzBld0djbkJWZXc1VkZaT3ZWaGdSQ3VOQWYiLCJ0eXBlIjoic2VsZiIsImV4cCI6MjI0OTA0OTkwNn0.Xc3KBM8bxLcCm0Z6_YR0JObn4yR9h4EjwG0d-KafWYE'
    ws = websocket.WebSocketApp("wss://ek.hongen.com/websocket/page/showhtml/index?info_id=1",
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close,
                                header=[header_auth])
    ws.on_open = on_open
    try:
        ws.run_forever()
    except Exception as e:
        print(e)
